{{if false}}<script>{{/if}}
{{$controller = $smarty.const.CCTRL}}
{{$token = $smarty.const.TOKEN}}
{{include file="../00_base/_function.tpl"}}
function handleEvents(){
    try{
        acolumns['_id'].cellsrenderer = function (row, column, value) {
            return "<span class='a lh24 pl8' title='"+value+"'>" + value.substring(0, 10).toUpperCase() + "</span>";
        }
        acolumns['_name'].validation= function (cell, value) {
            if (value =="") {
                return { result: false, message: "Title should be non-empty." };
            }
            return true;
        };
        myGrid.bind('cellendedit', function (event) {
            try{
                var args = event.args;
                var _column = args.datafield ,_row = args.rowindex,_value = args.value;
                var _data=myGrid.jqxGrid('getrowdata', _row);
                var _id=_data._id;
                if(_id==undefined || _id==""){
                    return;
                }
                switch(_column){
                    case "_status":
                        updateCol(_column,_id,_value);
                        break;
                    case "_name":
                        if(_value!=_data.Title)updateCol(_column,_id,_value);
                        break;
                    default:
                    uiMessage("Unkown this cell. Maybe 'editable' of this colums is 'off'."); 
                }
            }catch(e){
                uiMessage(e.message);
                console.log("Error:"+e.message);
            }
        });
    }catch(e){tipMessage(e.message);}
}
function SaveItem(r){
    try{
        if(isrunning)return;
        console.log("Save ↵ Call");
        
        var _imgs=new Array(),_tits=new Array();
        $(".AlbumItem img").map(function() {
            _imgs.push(new Array($(this).attr("src"),$(this).attr("title")));
        });
        var url="{{base_url()}}backend/{{$controller}}/save";
        var data={
            _id             :   $("#_id").val(),
            _name           :   $("#_name").val(),
            _alias          :   $("#_alias").val(),
            _thumb          :   $("#_thumb").val(),
            _des            :   $("#_des").val(),
            _imgs           :   _imgs
        }
        //ShowNoticeDialogMessage($("<div />").text(Content).html());return;
        console.log(data);
        isrunning=true;
        myAjax(url,data,function(data){
            isrunning=false;
            if(data.result>=0){
                uiMessage(data.message)
                if(r==undefined){
                    myjqxGrid.CancelEdit();
                    myjqxGrid.Refresh();
                }
            }else{
                ShowErrorDialogMessage("Error : "+data.message);
            }
        });
    }catch(e){
        console.log("JSON Error:"+e.message+"\n↵ Error");
        ShowErrorDialogMessage(e.message);
        //ShowErrorDialogMessage("Sorry. Your request could not be completed.<br/> Please check your input data.");
    }
}
function DelAlbumItem(obj){
    $(obj).parents("div.AlbumItem").remove();
}
function AddAlbumItem(){
    var AlbumItems =$("#AlbumItems");
    var srcImg=$("#txtAddImage").val();
    var desImg=$("#txtImgDes").val();
    if(!_FcheckFilled(srcImg)){return}
    var AlbumItem=
    '<div class="AlbumItem grid_5 mb4 mt4 ml4 mr4 bdccc gadiend-white">\
        <div class="pt1 pb1 pl1 pr1">\
            <div class="pl8 pt4 pb4 pr8 ovfh mt0 mb0 mr0 ml0 bdbccc">\
                Album item\
            </div>\
            <div class="pa r8 t4">\
                <span class="hover50" style="cursor: pointer" onclick="DelAlbumItem(this)">✖</span>\
            </div>\
            <div class="pl4 pr4 pt4 pb4 mt0 mb0 ml0 mr0 ovfa pr" style="height: 200px">\
                <img title="'+desImg+'" src="'+srcImg+'"/>\
            </div>\
        </div>\
    </div>';
    AlbumItems.append(AlbumItem);
    $("#txtAddImage").val("");
    $("#txtImgDes").val("");
    $("#txtAddImage").focus();
}